package com.jstyle.beatecg.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import com.jstyle.beatecg.model.MyBluetoothDevice;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SharePreferenceUtils {
    public static final String KEY_REMIND_PSD = "REMIND_PSD_beatecg";
    public static final String KEY_PHONE = "PHONE_beatecg";
    public static final String KEY_PSD = "PSD_beatecg";
    public static final String KEY_ADDRESS = "ADDRESS_beatecg";
    public static final String KEY_Language = "Language_beatecg";
    public static final String KEY_LOGIN = "lOGIN_beatecg";
    public static final String KEY_NICKNAME = "nickname_beatecg";
    private static final String spName = "jstyle_sleep_beatecg";
    public static final String KEY_AUTH_USERID = "KEY_AUTH_USERID_beatecg";
    public static final String KEY_highHeart = "highHeart";
    public static final String KEY_highBrEATH = "highBrEATH";
    public static final String KEY_lowHeart = "lowHeart";
    public static final String KEY_lowBreath = "lowBreath";
    public static final String account_default = "account_default_beatecg";

    public static final String KEY_vedio = "KEY_vedio";
    public static final String KEY_youhua = "KEY_youhua";
    public static final String KEY_stop= "KEY_stop";
    public static final String KEY_map = "KEY_map";


    private static SharedPreferences sp;

    public static void init(Context context) {
        sp = context.getSharedPreferences(spName, Activity.MODE_PRIVATE);
    }

    public static SharedPreferences getSharedPreferences() {
        return sp;
    }

    public static void setSpBoolean(String name, boolean bool) {

        getSharedPreferences().edit().putBoolean(name, bool).commit();
    }

    public static boolean getSpBoolean( String name) {
        return getSharedPreferences().getBoolean(name, name.equals(GlobalConast.KEY_isFirst));
    }

    public static void setSpString(String name, String value) {
        getSharedPreferences().edit().putString(name, value).commit();
    }

    public static String getSpString(String name) {
        return getSharedPreferences().getString(name, null);
    }

    public static void setSpInteger(String name, int value) {
        getSharedPreferences().edit().putInt(name, value).commit();
    }

    public static int getSpInteger(String name) {
        return getSharedPreferences().getInt(name, -1);
    }

    public static void setSpFloat(String name, float value) {
        getSharedPreferences().edit().putFloat(name, value).commit();
    }

    public static float getSpFloat(String name) {
        return getSharedPreferences().getFloat(name, 0.0f);
    }


    /**
     * 保存对象
     * @param user
     */
    public static void saveMyBluetoothDevice(Context context, String key,MyBluetoothDevice user)  {
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences(key, context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(user);//把对象写到流里
            String temp = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            editor.putString(key, temp);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 读取对象
     * @param context
     * @param key
     * @return
     */
    public static MyBluetoothDevice getUser(Context context,String key) {
        SharedPreferences sharedPreferences=context.getSharedPreferences(key,context.MODE_PRIVATE);
        String temp = sharedPreferences.getString(key, "");
        ByteArrayInputStream bais =  new ByteArrayInputStream(Base64.decode(temp.getBytes(), Base64.DEFAULT));
        MyBluetoothDevice user = null;
        try {
            ObjectInputStream ois = new ObjectInputStream(bais);
            user = (MyBluetoothDevice) ois.readObject();
        } catch (Exception e) { e.printStackTrace(); }
        return user;
    }

}
